# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2019/2/22 15:28
desc:
'''
import requests,unittest,logging,json
import base.globalvars as glo
from jsonpath import jsonpath
from util.date_util import get_datetime_from_now
from util.jsonmatch import my_list_cmp
from base import decorators
from proj_spec.inews.user_admin import get_user_id
from proj_spec.inews.news import get_news_tags,tag_table_info


class TestNews(unittest.TestCase):

    def setUp(self):
        pass

    @decorators.Smoke
    #@unittest.skip("current placeholder case")
    def test_news_detail(self):
        """验证新闻列表中的新闻都能正确访问到详情页面"""
        #Todo:该接口部分非必填参数缺省时，返回空列表
        def validate_tags(tagtype,newsid):
            exp_tags = get_news_tags(tagtype,newsid)
            act_tags = jsonpath(r_detail_json,'$.message.%s'%tag_table_info[tag_table_info['tag_name']==tagtype]['tag_path'].iloc[0])
            if act_tags is False or act_tags==[None]:
                act_tags = []
            self.assertTrue(set(exp_tags)==(set(act_tags)),"%s:%s标签与数据库不一致, expected:%s, actual:%s"%(newsid,tagtype,exp_tags,act_tags))

        # 请求新闻列表接口
        uid = get_user_id(glo.get_value("pay_user"))
        headers = {
            "uid":str(uid)
        }
        news_list_url = "/webui/news/page"
        root_url = glo.get_value("url1")
        url = root_url + news_list_url

        # 按页面默认值，获取最近一个月所有市场的新闻
        data = {
        "lang":"zh",
        "sort":"tim",
        "st": get_datetime_from_now(floor_time=True,format="%Y%m%d%H%M%S",months=-1),
        "dt": get_datetime_from_now(format="%Y%m%d%H%M%S"),
        "markets":"A,HK,OTC,US,BOND,TECH,PRI,NONE"
        }
        r = requests.request("post",url=url,headers=headers,data=data)
        self.assertTrue(r.status_code == 200, "response status not equal to 200")
        r_json = r.json()
        #logging.debug(r_json)

        json_path = "$.message.content[*].id"
        news_ids = jsonpath(r_json,json_path)

        #news_ids = [match.value for match in match_paths]
        logging.debug(news_ids)

        for id in news_ids:
            news_detail_url = root_url + "/webui/news/detail?id=%s"%id
            r_detail = requests.request("get", url=news_detail_url, headers=headers)
            r_detail_json = r_detail.json()
            titlezh = jsonpath(r_detail_json,'$.message.titlezh')
            self.assertTrue(titlezh != [], "新闻详情页没有标题： %s"%id)
            conzh = jsonpath(r_detail_json,'$.message.conzh')
            self.assertTrue(conzh != [], "新闻详情页没有内容： %s" % id)
            summaryzh = jsonpath(r_detail_json, '$.message.summaryzh')
            self.assertTrue(summaryzh != [], "新闻详情页没有摘要： %s" % id)
            # exp_event_tags = get_news_tags("event",id)
            # act_event_tags = jsonpath(r_detail_json,'$.message.events[*].name')
            # if act_event_tags is False or act_event_tags==[None]:
            #     act_event_tags = []
            # self.assertTrue(my_list_cmp(exp_event_tags,act_event_tags),"事件标签与数据库不一致")

            for tag in list(tag_table_info['tag_name']):
                validate_tags(tag,id)










